#!/bin/bash
# 备份/usr/etc/jkzuc的数据,最多备份两份

backup_jkzuc(){
    source_folder="/usr/etc/jkzuc"
    backup_folder="/home/jakauser"
    mkdir -p $backup_folder
    if [ -d $source_folder ];then
        # 备份开始
        backup_suffix=$(date +"%Y%m%d%H%M%S")
        backup_tar="${backup_folder}/jkzuc_${backup_suffix}.tar.gz"
        echo "Backup ... ${backup_tar}"
        tar -czf ${backup_tar} --exclude $source_folder/scripts/jkzuc -C ${backup_folder} $source_folder

        # 检查是否已进行两次备份,并删除最早的备份文件夹
        backup_count=$(ls -d ${backup_folder}/jkzuc_* | wc -l)
        if [ ${backup_count} -gt 2 ]; then    
            oldest_backup=$(ls -dt ${backup_folder}/jkzuc_* | tail -1)
            rm -rf "${oldest_backup}"
            echo "Clean the oldest backup ${oldest_backup}"
        fi
    fi
}

set -e
# 目前仅对jkzuc文件夹进行整体备份，后续再处理老控制器的分散在别的地方的文件
backup_jkzuc

# 清理老环境
rm -rf $source_folder/configs/common
